//
//  ApplicationHandler.m
//  YSSmartHome
//
//  Created by yanghao on 2017/8/13.
//  Copyright © 2017年 YangHao. All rights reserved.
//

#import "ApplicationHandler.h"
#import <Bugly/Bugly.h>
#import <iflyMSC/IFlyMSC.h>
#import "YHMarcros.h"
#import "NetworkMonitorUtils.h"
#import "DataBaseManager.h"
#import "GlobalConst.h"

static NSString * const kZTBUGLYAPPID  = @"752a013e80";
static NSString * const kZTIFLYAPPID = @"5832afc0";
static NSString * const kZTJPUSHSERVICEAPPKEY = @"72a591946a36b83cafeb5f6f";
static NSString * const kZTJPUSHSERVICECHANNEL = @"App Store";

@interface ApplicationHandler () <BuglyDelegate>

@end
@implementation ApplicationHandler

static ApplicationHandler* instance = nil;

+ (instancetype)shared {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc]init];
    });
    return instance;
}

+ (id)allocWithZone: (struct _NSZone*)zone {
    if(instance == nil){
        static dispatch_once_t once;
        dispatch_once(&once, ^{
            instance = [super allocWithZone:zone];
        });
    }
    return instance;
}

- (void)launching {
    [self prepareLoad];
}

- (void)prepareLoad{
    [self setupisFirstLaunch];
    [self handleNetworkRequest];
    [self registerLibrary];
    [self configAppearance];
    [self ignoreSIGPIPE];
    [DataBaseManager creatTableIfNeed];
}

- (void)handleNetworkRequest {
    [[NetworkMonitorUtils shared] startListen];
}
- (void)registerLibrary{
    [self setupIfly];
    [self setupBugly];
}
- (void)configAppearance
{
    //UINavigationBar
    UINavigationBar *navigationBar = [UINavigationBar appearance];
//    navigationBar.translucent = NO;
    NSMutableDictionary* barTextAttrs = [NSMutableDictionary dictionary];
    barTextAttrs[NSForegroundColorAttributeName] = RGBColor(51,51,51);
    barTextAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:17];
    [navigationBar setTitleTextAttributes:barTextAttrs];
    [navigationBar setBarTintColor:[UIColor whiteColor]];
    //UIBarButtonItem
    UIBarButtonItem *barButtonItem = [UIBarButtonItem appearance];
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = RGBColor(102, 102, 102);
    [barButtonItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    //UITabBar
    [[UITabBar appearance] setShadowImage:[UIImage new]];
    [[UITabBar appearance] setBackgroundImage:[[UIImage alloc]init]];
    //UITextField
    [[UITextField appearance] setTintColor:RGBColor(29, 171, 250)];
}
- (void)setupIfly
{
    //设置sdk的log等级，log保存在下面设置的工作路径中
    [IFlySetting setLogFile:LVL_ALL];
    //打开输出在console的log开关
    [IFlySetting showLogcat:NO];
    //设置sdk的工作路径
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = [paths objectAtIndex:0];
    [IFlySetting setLogFilePath:cachePath];
    //创建语音配置,appid必须要传入，仅执行一次则可
    NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",kZTIFLYAPPID];
    //所有服务启动前，需要确保执行createUtility
    [IFlySpeechUtility createUtility:initString];
}

- (void)setupBugly {
    BuglyConfig * config = [[BuglyConfig alloc] init];
#if DEBUG
    config.debugMode = YES;
#endif
    config.reportLogLevel = BuglyLogLevelWarn;
    config.blockMonitorEnable = YES;
    config.blockMonitorTimeout = 1.5;
    config.channel = @"Bugly";
    config.delegate = self;
    [Bugly startWithAppId:kZTBUGLYAPPID
#if DEBUG
        developmentDevice:YES
#endif
                   config:config];
    [Bugly setUserIdentifier:[NSString stringWithFormat:@"User: %@", [UIDevice currentDevice].name]];
}

- (void)ignoreSIGPIPE {
    signal(SIGPIPE, SIG_IGN);
}

- (void)setupisFirstLaunch
{
    NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
    if(![userDefaults boolForKey:@"firstLaunch"]){
        [userDefaults setBool:YES forKey:@"firstLaunch"];
        //第一次启动
        [userDefaults setBool:YES forKey:kZTShockSwitchIsOpen];
    }else{
        //不是第一次启动了
    }
}
@end
